home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 321 / compsrc3 / stupid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  13.8 KB  |  446 lines

  1. /* Dummy data flow analysis for GNU compiler in nonoptimizing mode.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. /* This file performs stupid register allocation, which is used
  23.    when cc1 gets the -noreg switch (which is when cc does not get -O).
  24.  
  25.    Stupid register allocation goes in place of the the flow_analysis,
  26.    local_alloc and global_alloc passes.  combine_instructions cannot
  27.    be done with stupid allocation because the data flow info that it needs
  28.    is not computed here.
  29.  
  30.    In stupid allocation, the only user-defined variables that can
  31.    go in registers are those declared "register".  They are assumed
  32.    to have a life span equal to their scope.  Other user variables
  33.    are given stack slots in the rtl-generation pass and are not
  34.    represented as pseudo regs.  A compiler-generated temporary
  35.    is assumed to live from its first mention to its last mention.
  36.  
  37.    Since each pseudo-reg's life span is just an interval, it can be
  38.    represented as a pair of numbers, each of which identifies an insn by
  39.    its position in the function (number of insns before it).  The first
  40.    thing done for stupid allocation is to compute such a number for each
  41.    insn.  It is called the suid.  Then the life-interval of each
  42.    pseudo reg is computed.  Then the pseudo regs are ordered by priority
  43.    and assigned hard regs in priority order.  */
  44.  
  45. #include <stdio.h>
  46. #include "config.h"
  47. #include "rtl.h"
  48. #include "hard-reg-set.h"
  49. #include "regs.h"
  50.  
  51. /* Vector mapping INSN_UIDs to suids.
  52.    The suids are like uids but increase monononically always.
  53.    We use them to see whether a subroutine call came
  54.    between a variable's birth and its death.  */
  55.  
  56. static short *uid_suid;
  57.  
  58. /* Get the suid of an insn.  */
  59.  
  60. #define INSN_SUID(INSN) (uid_suid[INSN_UID (INSN)])
  61.  
  62. /* Record the suid of the last CALL_INSN
  63.    so we can tell whether a potential combination crosses any calls.  */
  64.  
  65. static int last_call_suid;
  66.  
  67. /* Element N is suid of insn where life span of pseudo reg N ends.
  68.    Element is  0 if register N has not been seen yet on backward scan.  */
  69.  
  70. static short *reg_where_dead;
  71.  
  72. /* Element N is suid of insn where life span of pseudo reg N begins.  */
  73.  
  74. static short *reg_where_born;
  75.  
  76. /* Numbers of pseudo-regs to be allocated, highest priority first.  */
  77.  
  78. static short *reg_order;
  79.  
  80. /* Indexed by reg number (hard or pseudo), nonzero if register is live
  81.    at the current point in the instruction stream.  */
  82.  
  83. static char *regs_live;
  84.  
  85. /* Indexed by insn's suid, the set of hard regs live after that insn.  */
  86.  
  87. static HARD_REG_SET *after_insn_hard_regs;
  88.  
  89. /* Record that hard reg REGNO is live after insn INSN.  */
  90.  
  91. #define MARK_LIVE_AFTER(INSN,REGNO)  \
  92.   SET_HARD_REG_BIT (after_insn_hard_regs[INSN_SUID (INSN)], (REGNO))
  93.  
  94. static void stupid_mark_refs ();
  95. static int stupid_reg_compare ();
  96. static int stupid_find_reg ();
  97.  
  98. /* Stupid life analysis is for the case where only variables declared
  99.    `register' go in registers.  For this case, we mark all
  100.    pseudo-registers that belong to register variables as
  101.    dying in the last instruction of the function, and all other
  102.    pseudo registers as dying in the last place they are referenced.
  103.    Hard registers are marked as dying in the last reference before
  104.    the end or before each store into them.  */
  105.  
  106. void
  107. stupid_life_analysis (f, nregs, file)
  108.      rtx f;
  109.      int nregs;
  110.      FILE *file;
  111. {
  112.   register int i;
  113.   register rtx last, insn;
  114.   int max_uid;
  115.  
  116.   bzero (regs_ever_live, sizeof regs_ever_live);
  117.  
  118.   regs_live = (char *) alloca (nregs);
  119.  
  120.   /* First find the last real insn, and count the number of insns,
  121.      and assign insns their suids.  */
  122.  
  123.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  124.     if (INSN_UID (insn) > i)
  125.       i = INSN_UID (insn);
  126.  
  127.   max_uid = i + 1;
  128.   uid_suid = (short *) alloca ((i + 1) * sizeof (short));
  129.  
  130.   /* Compute the mapping from uids to suids.
  131.      Suids are numbers assigned to insns, like uids,
  132.      except that suids increase monotonically through the code.  */
  133.  
  134.   last = 0;            /* In case of empty function body */
  135.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  136.     {
  137.       if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
  138.       || GET_CODE (insn) == JUMP_INSN)
  139.     last = insn;
  140.       INSN_SUID (insn) = ++i;
  141.     }
  142.  
  143.   last_call_suid = 0;
  144.  
  145.   max_regno = nregs;
  146.  
  147.   /* Allocate tables to record info about regs.  */
  148.  
  149.   reg_where_dead = (short *) alloca (nregs * sizeof (short));
  150.   bzero (reg_where_dead, nregs * sizeof (short));
  151.  
  152.   reg_where_born = (short *) alloca (nregs * sizeof (short));
  153.   bzero (reg_where_born, nregs * sizeof (short));
  154.  
  155.   reg_order = (short *) alloca (nregs * sizeof (short));
  156.   bzero (reg_order, nregs * sizeof (short));
  157.  
  158.   reg_renumber = (short *) oballoc (nregs * sizeof (short));
  159.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  160.     reg_renumber[i] = i;
  161.  
  162.   after_insn_hard_regs = (HARD_REG_SET *) alloca (max_uid * sizeof (HARD_REG_SET));
  163.   bzero (after_insn_hard_regs, max_uid * sizeof (HARD_REG_SET));
  164.  
  165.   /* Allocate and zero out many data structures
  166.      that will record the data from lifetime analysis.  */
  167.  
  168.   allocate_for_life_analysis ();
  169.  
  170.   for (i = 0; i < max_regno; i++)
  171.     {
  172.       reg_n_deaths[i] = 1;
  173.     }
  174.  
  175.   bzero (regs_live, nregs);
  176.  
  177.   /* Find where each pseudo register is born and dies,
  178.      by scanning all insns from the end to the start
  179.      and noting all mentions of the registers.
  180.  
  181.      Also find where each hard register is live
  182.      and record that info in after_insn_hard_regs.
  183.      regs_live[I] is 1 if hard reg I is live
  184.      at the current point in the scan.  */
  185.  
  186.   for (insn = last; insn; insn = PREV_INSN (insn))
  187.     {
  188.       register HARD_REG_SET *p = after_insn_hard_regs + INSN_SUID (insn);
  189.  
  190.       /* Copy the info in regs_live
  191.      into the element of after_insn_hard_regs
  192.      for the current position in the rtl code.  */
  193.  
  194.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  195.     if (regs_live[i])
  196.       SET_HARD_REG_BIT (*p, i);
  197.  
  198.       /* Mark all call-clobbered regs as live after each call insn
  199.      so that a pseudo whose life span includes this insn
  200.      will not go in one of them.
  201.      Then mark those regs as all dead for the continuing scan
  202.      of the insns before the call.  */
  203.  
  204.       if (GET_CODE (insn) == CALL_INSN)
  205.     {
  206.       last_call_suid = INSN_SUID (insn);
  207.       IOR_HARD_REG_SET (after_insn_hard_regs[last_call_suid],
  208.                 call_used_reg_set);
  209.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  210.         if (call_used_regs[i])
  211.           regs_live[i] = 0;
  212.     }
  213.  
  214.       /* Update which hard regs are currently live
  215.      and also the birth and death suids of pseudo regs
  216.      based on the pattern of this insn.  */
  217.  
  218.       if (GET_CODE (insn) == INSN
  219.       || GET_CODE (insn) == CALL_INSN
  220.       || GET_CODE (insn) == JUMP_INSN)
  221.     {
  222.       stupid_mark_refs (PATTERN (insn), insn);
  223.     }
  224.     }
  225.  
  226.   /* Now decide the order in which to allocate the pseudo registers.  */
  227.  
  228.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  229.     reg_order[i] = i;
  230.  
  231.   qsort (®_order[FIRST_PSEUDO_REGISTER],
  232.      max_regno - FIRST_PSEUDO_REGISTER, sizeof (short),
  233.      stupid_reg_compare);
  234.  
  235.   /* Now, in that order, try to find hard registers for those pseudo regs.  */
  236.  
  237.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  238.     {
  239.       register int r = reg_order[i];
  240.       enum reg_class class;
  241.  
  242.       /* Now find the best hard-register class for this pseudo register */
  243.       if (N_REG_CLASSES > 1)
  244.     class = reg_preferred_class (r);
  245.  
  246.       reg_renumber[r] = stupid_find_reg (reg_crosses_call[r], class,
  247.                      PSEUDO_REGNO_MODE (r),
  248.                      reg_where_born[r],
  249.                      reg_where_dead[r]);
  250.  
  251.       /* If no reg available in that class,
  252.      try any reg.  */
  253.       if (reg_renumber[r] == -1)
  254.     reg_renumber[r] = stupid_find_reg (reg_crosses_call[r], GENERAL_REGS,
  255.                        PSEUDO_REGNO_MODE (r),
  256.                        reg_where_born[r],
  257.                        reg_where_dead[r]);
  258.     }
  259.  
  260.   if (file)
  261.     dump_flow_info (file);
  262. }
  263.  
  264. /* Comparison function for qsort.
  265.    Returns -1 (1) if register *R1P is higher priority than *R2P.  */
  266.  
  267. static int
  268. stupid_reg_compare (r1p, r2p)
  269.      short *r1p, *r2p;
  270. {
  271.   register int r1 = *r1p, r2 = *r2p;
  272.   register int len1 = reg_where_dead[r1] - reg_where_born[r1];
  273.   register int len2 = reg_where_dead[r2] - reg_where_born[r2];
  274.  
  275.   if (len1 != len2)
  276.     return len2 - len1;
  277.  
  278.   return reg_n_refs[r1] - reg_n_refs[r2];
  279. }
  280.  
  281. /* Find a block of SIZE words of hard registers in reg_class CLASS
  282.    that can hold a value of machine-mode MODE
  283.      (but actually we test only the first of the block for holding MODE)
  284.    currently free from after insn whose suid is BIRTH
  285.    through the insn whose suid is DEATH,
  286.    and return the number of the first of them.
  287.    Return -1 if such a block cannot be found.
  288.    If CALL_PRESERVED is nonzero, insist on registers preserved
  289.    over subroutine calls, and return -1 if cannot find such.  */
  290.  
  291. static int
  292. stupid_find_reg (call_preserved, class, mode,
  293.          born_insn, dead_insn)
  294.      int call_preserved;
  295.      enum reg_class class;
  296.      enum machine_mode mode;
  297.      int born_insn, dead_insn;
  298. {
  299.   register int i, ins;
  300. #ifdef HARD_REG_SET
  301.   register        /* Declare them register if they are scalars.  */
  302. #endif
  303.     HARD_REG_SET used, this_reg;
  304.  
  305.   COPY_HARD_REG_SET (used,
  306.              call_preserved ? call_used_reg_set : fixed_reg_set);
  307.  
  308.   for (ins = born_insn; ins < dead_insn; ins++)
  309.     IOR_HARD_REG_SET (used, after_insn_hard_regs[ins]);
  310.  
  311.   IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  312.  
  313.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  314.     if (! TEST_HARD_REG_BIT (used, i)
  315.     && HARD_REGNO_MODE_OK (i, mode))
  316.       {
  317.     register int j;
  318.     register int size1 = HARD_REGNO_NREGS (i, mode);
  319.     for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, i + j); j++);
  320.     if (j == size1)
  321.       {
  322.         CLEAR_HARD_REG_SET (this_reg);
  323.         while (--j >= 0)
  324.           SET_HARD_REG_BIT (this_reg, i + j);
  325.         for (ins = born_insn; ins < dead_insn; ins++)
  326.           {
  327.         IOR_HARD_REG_SET (after_insn_hard_regs[ins], this_reg);
  328.           }
  329.         return i;
  330.       }
  331.     i += j;            /* Skip starting points we know will lose */
  332.       }
  333.   return -1;
  334. }
  335.  
  336. /* Walk X, noting all assignments and references to registers
  337.    and recording what they imply about life spans.
  338.    INSN is the current insn, supplied so we can find its suid.  */
  339.  
  340. static void
  341. stupid_mark_refs (x, insn)
  342.      rtx x, insn;
  343. {
  344.   register RTX_CODE code = GET_CODE (x);
  345.   register char *fmt;
  346.   register int regno, i;
  347.  
  348.   if (code == SET || code == CLOBBER)
  349.     {
  350.       if (SET_DEST (x) != 0 && GET_CODE (SET_DEST (x)) == REG)
  351.     {
  352.       /* Register is being assigned.  */
  353.       regno = REGNO (SET_DEST (x));
  354.  
  355.       /* For hard regs, update the where-live info.  */
  356.       if (regno < FIRST_PSEUDO_REGISTER)
  357.         {
  358.           register int j
  359.         = HARD_REGNO_NREGS (regno, GET_MODE (SET_DEST (x)));
  360.           while (--j >= 0)
  361.         {
  362.           regs_ever_live[regno+j] = 1;
  363.           regs_live[regno+j] = 0;
  364.           /* The following line is for unused outputs;
  365.              they do get stored even though never used again.  */
  366.           MARK_LIVE_AFTER (insn, regno);
  367.         }
  368.         }
  369.       /* For pseudo regs, record where born, where dead, number of
  370.          times used, and whether live across a call.  */
  371.       else
  372.         {
  373.           /* Update the life-interval bounds of this reg.  */
  374.           reg_where_born[regno] = INSN_SUID (insn);
  375.  
  376.           /* The reg must live at least one insn even
  377.          if it is never again used--because it has to go
  378.          in SOME hard reg.  */
  379.           if (reg_where_dead[regno] < INSN_SUID (insn) + 1)
  380.         reg_where_dead[regno] = INSN_SUID (insn) + 1;
  381.  
  382.           /* Count the refs of this reg.  */
  383.           reg_n_refs[regno]++;
  384.  
  385.           if (last_call_suid < reg_where_dead[regno])
  386.         reg_crosses_call[regno] = 1;
  387.         }
  388.     }
  389.       /* Record references from the value being set,
  390.      or from addresses in the place being set if that's not a reg.
  391.      If setting a SUBREG, we treat the entire reg as *used*.  */
  392.       if (code == SET)
  393.     {
  394.       stupid_mark_refs (SET_SRC (x), insn);
  395.       if (GET_CODE (SET_DEST (x)) != REG)
  396.         stupid_mark_refs (SET_DEST (x), insn);
  397.     }
  398.       return;
  399.     }
  400.  
  401.   /* Register value being used, not set.  */
  402.  
  403.   if (code == REG)
  404.     {
  405.       regno = REGNO (x);
  406.       if (regno < FIRST_PSEUDO_REGISTER)
  407.     {
  408.       /* Hard reg: mark it live for continuing scan of previous insns.  */
  409.       register int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
  410.       while (--j >= 0)
  411.         {
  412.           regs_ever_live[regno+j] = 1;
  413.           regs_live[regno+j] = 1;
  414.         }
  415.     }
  416.       else
  417.     {
  418.       /* Pseudo reg: record first use, last use and number of uses.  */
  419.  
  420.       reg_where_born[regno] = INSN_SUID (insn);
  421.       reg_n_refs[regno]++;
  422.       if (regs_live[regno] == 0)
  423.         {
  424.           regs_live[regno] = 1;
  425.           reg_where_dead[regno] = INSN_SUID (insn);
  426.         }
  427.     }
  428.       return;
  429.     }
  430.  
  431.   /* Recursive scan of all other rtx's.  */
  432.  
  433.   fmt = GET_RTX_FORMAT (code);
  434.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  435.     {
  436.       if (fmt[i] == 'e')
  437.     stupid_mark_refs (XEXP (x, i), insn);
  438.       if (fmt[i] == 'E')
  439.     {
  440.       register int j;
  441.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  442.         stupid_mark_refs (XVECEXP (x, i, j), insn);
  443.     }
  444.     }
  445. }
  446.